<?php

namespace addons\qingdong\library;

use fast\Http;
use fast\Random;
use think\Model;

class Jing extends Model
{
    protected $developHost="https://nyac-baize-api.staging.ukuaiqi.com";
    protected $relaseHost="https://baize-api-nyac.jingxiansuo.com";

    protected $key="QsKqJxsVEssY74UnturnedRcvNUOYU";
    protected $host="";
    protected $compamey_id="41908265";
    protected $user_id="79385142";
    protected  $header=[
        'Content-Type:'. "application/json",
    ];
    private $notifyUrl="";
    public function __construct()
    {
        $this->host=$this->developHost;
        $this->notifyUrl=request()->domain().'/addons/qingdong/qingdong/Jing/notify';
        return parent::__construct();
    }

    /*账户打通*/
    public function openAccount($companey)
    {
        $path="security/openAccount";
        $data=array();
        $data['username']=$companey;
        $data['companyName']=$companey;
        $data['monthlyQuota']=-1;
        $data['content']=$this->getContent();
        $header=$this->header;
        $res=httpCurl::postData(json_encode($data),$header,$this->host."/".$path);
        $result=json_decode($res,true);
        if($result['errcode']=="200" && $result['errmsg']=="Success"){
            $resultData=$result['data'];
            return $resultData;
        }
        return $result['errmsg'];
//      account: 11000013080
//      password: hlTRghyd
    }
    /*鉴权*/
    public function login($method,$source="web")
    {
        if(!$method){
            return false;
        }
        $path="security/open/login";
        $header=$this->header;
        $data=array();
        $data['content']=$this->getContent("searchCustomer/company-search");
        $data['pushUrl']=$this->notifyUrl."/m/".$method;
        $res=httpCurl::postData(json_encode($data),$header,$this->host."/".$path);
        $result=json_decode($res,true);
        if($result['errcode']=="200" && $result['errmsg']=="Success"){
            $resultData=$result['data'];
            return $resultData;
        }
        return $result['errmsg'];
    }
    /*删除用户*/
    public function delUser()
    {
        $path="security/open/deleteUser";
        $header=$this->header;
    }

    private function getContent($method="")
    {
        $companey_id=$this->compamey_id?$this->compamey_id:Random::numeric(8);
        $user_id=$this->user_id?$this->user_id:Random::numeric(8);
        $content=$companey_id.":".$user_id;
        $content=$method?$content.$method:$content;
        return $this->AESEncrypt($content);
    }
    /**
     * $plaintext,明文
     * $Key,密钥
     * $type enc加密，dec解密
     * */
    private function AESEncrypt($plaintext, $key="", $type = 'enc')
    {
        try {
            if (!$key) {
                $key=$this->key;
            }

            //截取字符串的MD5值的8-24位
            $key = substr(strtoupper(md5($key)), 8, 16);

            if ($type == 'enc') {
                //加密
                $value = openssl_encrypt($plaintext, 'AES-128-ECB', $key, 0);
            } else if ($type == 'dec') {
                //解密
                $value = openssl_decrypt($plaintext, 'AES-128-ECB', $key, 0);
            }

            return $value;

        } catch (\Exception $e) {
            echo $e->getMessage();
        }
    }


    //url安全
    private function urlSafe($value)
    {
        $value = str_replace("[+]","-",$value);
        $value = str_replace("/","_",$value);
        $value = str_replace("=","",$value);
        $value = str_replace("\r|\n","",$value);
        return $value;
    }

    //url解析
    private function urlUnSafe($value) {
        $value = str_replace("-","+",$value);
        $value = str_replace("_","/",$value);

        $strlen = (strlen($value) * 3 ) % 4;

        if ($strlen > 0) {
            for($i=0;$i<$strlen;$i++){
                $value .= '=';
            }
        }

        return $value;
    }
}